import empyrical


class Analyzer(object):
    def __init__(self, cerebro):
        self.cerebro = cerebro

    def result(self):
        snapshots = self.cerebro.broker.snapshot.daily()
        returns = (snapshots.value - snapshots.value.shift(1)) / snapshots.value.shift(1)
        returns = returns.fillna(0)
        return dict(
            fund=self.cerebro.broker.fund, value=self.cerebro.broker.equity,
            commission=self.cerebro.broker.commission,
            profit=self.cerebro.broker.equity - self.cerebro.broker.fund,
            annual_return=empyrical.annual_return(returns),
            annual_volatility=empyrical.annual_volatility(returns),
            cagr=empyrical.cagr(returns),
            calmar_ratio=empyrical.calmar_ratio(returns),
            max_drawdown=empyrical.max_drawdown(returns),
            omega_ratio=empyrical.omega_ratio(returns),
            sharpe_ratio=empyrical.sharpe_ratio(returns),
            sortino_ratio=empyrical.sortino_ratio(returns),
            downside_risk=empyrical.downside_risk(returns),
            tail_ratio=empyrical.tail_ratio(returns),
            r_squared=empyrical.stability_of_timeseries(returns)
        )


__all__ = [Analyzer]
